package controllers

import (
	"news/common/response"
	"news/logic"
	"news/models"

	"go.uber.org/zap"

	"github.com/go-playground/validator/v10"

	"github.com/gin-gonic/gin"
)

func PostVoteHandler(c *gin.Context) {
	p := new(models.ParamVoteDataForm)
	if err := c.ShouldBindJSON(p); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.ResponseErr(c, response.CodeInvalidParam)
			return
		}
		errData := removeTopStruct(errs.Translate(trans))
		response.ResponseErrorWithMsg(c, response.CodeInvalidParam, errData)
		return
	}
	//获取当前请求用户的ID
	userId, _, err := GetCurrentUserInfo(c)
	if err != nil {
		response.ResponseErr(c, response.CodeNeedLogin)
		return
	}
	if err := logic.VoteFormPost(userId, p); err != nil {
		zap.L().Error("logic.VoteFormPost(userId)", zap.Error(err))
		response.ResponseErr(c, response.CodeServerBusy)
		return
	}
	response.ResponseSuccess(c, nil)

}
